package com.flyee.controller.tools.cartoon.control
{
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	public class CartoonPlayRule
	{
		private var cartoonArray:Array = null;
		private var _autoPlay:Boolean = false;
		
		private var type:uint = 0;
		private var dir:uint = 0;
		private var frame:uint = 0;
		
		private var parentContainer:DisplayObjectContainer = null;
		
		private var cartoonContainer:Sprite = new Sprite();
		
		private var timer:Timer = null;
		public function CartoonPlayRule(t:Array,parentContainer:DisplayObjectContainer,delay:Number = 50)
		{
			cartoonArray = t;
			this.parentContainer = parentContainer;
			this.parentContainer.addChild(this.cartoonContainer)
			timer = new Timer(delay)
			timer.addEventListener(TimerEvent.TIMER,timerHandle);
		}
		public function set autoPlay(b:Boolean):void
		{
			this._autoPlay = b;
			if(this._autoPlay)
			{
				timer.reset();
				timer.start()
			}
			else
			{
				timer.stop();
			}
		}
		public function get autoPlay():Boolean
		{
			return this._autoPlay;
		}
		public function getType():uint
		{
			return this.type;
		}
		public function getDir():uint
		{
			return this.dir;
		}
		public function getFrame():uint
		{
			return this.frame;
		}
		public function setArray(t:Array):void
		{
			this.cartoonArray = t;
		}
		public function setData(type:uint,dir:uint,frame:uint = 0):void
		{
			//动画配置文件不完整 导致需要特殊处理
			this.type = type;
			this.dir = dir;
			this.frame = frame;
			
			//timer.reset();
			//this.timerHandle()
		}
		public function timerHandle(e:TimerEvent = null):void
		{
			while(this.cartoonContainer.numChildren > 0)
			{
				this.cartoonContainer.removeChildAt(0);
			}
			if(this.cartoonArray == null)
			{
				return;
			}
			this.type = this.type%this.cartoonArray.length;
			if(this.cartoonArray[type] == null)
			{
				return;
			}
			this.dir = this.dir%this.cartoonArray[type].length;
			if(this.cartoonArray[type][dir] == null)
			{
				return;
			}
			this.frame = this.frame%this.cartoonArray[type][dir].length;
			if(this.cartoonArray[type] == null)
			{
				this.cartoonContainer.addChild(this.cartoonArray[1][dir][0]);
			}
			else
			{
				this.cartoonContainer.addChild(this.cartoonArray[type][dir][frame]);
			}
			this.frame = this.frame+1
		}
		/**
		 *清除函数 
		 * 
		 */		
		public function clear():void
		{
			if(this.parentContainer.contains(this.cartoonContainer))
			{
				this.parentContainer.removeChild(this.cartoonContainer);
			}
			this.parentContainer = null;
			
			while(this.cartoonContainer.numChildren > 0)
			{
				this.cartoonContainer.removeChildAt(0);
			}
			this.cartoonContainer = null;
			
			this.cartoonArray.splice(0);
			this.cartoonArray = null;
			
			this.timer.stop()
			this.timer.removeEventListener(TimerEvent.TIMER,timerHandle);
			this.timer = null
		}
	}
}